package Service;

import Dao.ArticleAttachmentDAO;
import Dao.ArticleDAO;
import Dao.Impl.*;
import entity.Article;
import entity.ArticleAttachment;
import exception.BusinessException;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Author Su
 * @Date 2025/4/30 16:40
 * @Version 1.0 （版本号）
 */
public class AttachmentService {

    private ArticleDAO articleDAO;
    private ArticleAttachmentDAO attachmentDAO;
    private KnowledgeBaseService knowledgeBaseService;

    public AttachmentService(Connection connection) {
        this.articleDAO = new ArticleDAOImpl(connection);
        this.attachmentDAO = new ArticleAttachmentDAOImpl(connection);
        this.knowledgeBaseService = new KnowledgeBaseService(new KnowledgeBaseDAOImpl(connection),new KnowledgeBaseMemberDAOImpl(connection),new UserDAOImpl(connection));
    }
    public ArticleAttachment getAttachmentWithAuthCheck(int attachmentId, int userId)
            throws BusinessException, SQLException {
        // 1. 获取基础记录
        ArrayList<ArticleAttachment> attachment = attachmentDAO.getAttachmentByArticleId(attachmentId);
        if (attachment == null) {
            throw new BusinessException("附件不存在");
        }
//        // 2. 验证文章权限
//        Article article = articleDAO.getArticleById(attachment.getArticleId());
//        if (!knowledgeBaseService.hasAccess(article.getKnowledgeBaseId(), userId)) {
//            throw new BusinessException("无权访问此附件");
//        }
//        // 3. 验证文件物理存在
//        if (!new File(attachment.getFilePath()).exists()) {
//            throw new BusinessException("附件文件已丢失");
//        }

        return new ArticleAttachment();
    }
}